package com.translation.dict;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.translation.dao.Batch;

public class DictImporter {

	public static void main(String[] args) throws IOException, SQLException {
		BufferedReader r = null;
		try {
			r = new BufferedReader(new FileReader(args[0]));
			final BufferedReader r1 = r;
			Iterator<String[]> it = new Iterator<String[]>() {

				Pattern p = Pattern.compile("^([^0-9\\s]([^\\[\\]]*))(\\[[^\\[\\]]*\\])?");
				Pattern t = Pattern.compile("^[\\s]+(.+)$");
				String word = null;
				LinkedList<String> terms = new LinkedList<String>();

				@Override
				public void remove() {
				}

				@Override
				public String[] next() {
					String term = null;
					try {
						String line;

						if (terms.size() == 0) {
							word = null;
							while (word == null && (line = r1.readLine()) != null) {
								Matcher m = p.matcher(line);
								if (m.find()) {
									word = m.group(1);
									word = word.replaceAll("(\\(.*?\\)|\\[.*?\\]|\\p{Punct})", "").trim().toLowerCase();
								}
							}
							if (word != null) {
								String str = null;
								while (str == null && (line = r1.readLine()) != null) {
									line = line.replaceAll("(\\(.*?\\)|\\[.*?\\]|\\p{Punct})", "");
									Matcher m = t.matcher(line);
									if (m.find()) {
										str = m.group(1);
										String[] local = str.split("[;]+");
										for (int i = 0; i < local.length; i++) {
											String t = local[i].trim().replaceAll("[\\s]*\\[.*?\\][\\s]*", "");
											if (t.length() < 255) {
												terms.add(t.toLowerCase());
											}
										}
									}
								}
							}
						}
						if (terms.size() > 0) {
							term = terms.removeFirst();
						}
					} catch (Exception e) {
					}

					return (word != null && term != null) ? new String[]{
																			word,
																			term} : null;
				}

				@Override
				public boolean hasNext() {
					return true;
				}
			};

			String[] entry = null;
			while ((entry = it.next()) != null) {
				System.out.println(Arrays.toString(entry));
			}

			/*
			Batch batch = getBatch(it, args[1]);
			BaseDAO dao = new BaseDAO();
			dao.namedBatch("dict", batch);
			*/

		} finally {
			if (r != null) {
				r.close();
			}
		}
	}
	private static Batch getBatch(final Iterator<String[]> it, final String locale_id) {
		return new Batch() {

			private String[] word;

			@Override
			public void setParams(PreparedStatement st) throws SQLException {
				int index = 1;
				st.setString(index++, word[0].toLowerCase());
				st.setString(index++, word[1].toLowerCase());
				st.setString(index++, locale_id);
				st.setString(index++, "transl.");
			}

			@Override
			public boolean result(int[] result, PreparedStatement st) throws SQLException {
				return true;
			}

			@Override
			public boolean next() throws SQLException {
				word = it.next();
				return word != null;
			}

			@Override
			public int getSize() {
				return 100;
			}

			@Override
			public void error(SQLException e) {
				if (word != null) {
					System.out.println("Near:" + Arrays.toString(word));
				}
			}
		};
	}
}
